package com.thread;

import static java.lang.Thread.*;

public class ThreadMethod002 {
    static void testSleep(){
        //让出cpu 复活由时间而定
        new Thread(()->{
            for(int i=0;i<100;++i){
                System.out.println("i="+i);

                try {
                    Thread.sleep(2000);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    // yield 我让出cpu,回到等待队列，有可能还会被我调度
    static void testyield(){
        new Thread(()->{
            for(int i=0;i<100;++i){
                System.out.println("A i="+i);

                if(i%10==0)Thread.yield();

            }
        }).start();

        new Thread(()->{
            for(int i=0;i<100;++i){
                System.out.println("B i="+i);

                if(i%10==0)Thread.yield();
            }
        }).start();

    }

    static void testjoin(){
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 100; ++i) {
                System.out.println("A i=" + i);

                try {
                    Thread.sleep(100);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });


        Thread t2 = new Thread(() -> {
            try {
                t1.join();//等t1 调用完，我再调用
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            for (int i = 0; i < 100; ++i) {
                System.out.println("B i=" + i);
            }
        });

        t2.start();
        t1.start();

    }

    public static void main(String[] args) {
        //testSleep();
        //testyield();
        testjoin();
    }
}
